@echo off 
chcp 65001

set "micaPath=%CD%\Mica"

for /f "tokens=2 delims=: " %%a in ('findstr /r "executableName" "electron-builder.yml"') do (
    set "executableName=%%a"
)
for /f "tokens=2 delims=: " %%a in ('findstr /r "productName" "electron-builder.yml"') do (
    set "productName=%%a"
)


set "inputFile=%micaPath%\MicaSetup\Program_Example.cs"
set "outputFile=%micaPath%\MicaSetup\Program.cs"
 (for /f "tokens=*" %%a in ('type "%inputFile%" ^| find /v ""') do (
    set "line=%%a" 
    setlocal enabledelayedexpansion
    set "modifiedLine=!line:MicaApp=%executableName%!"
    echo !modifiedLine!
    endlocal
 ))  > "%outputFile%"
set "inputFile=%micaPath%\MicaSetup\Program.un_Example.cs"
set "outputFile=%micaPath%\MicaSetup\Program.un.cs"
 (for /f "tokens=*" %%a in ('type "%inputFile%" ^| find /v ""') do (
    set "line=%%a" 
    setlocal enabledelayedexpansion
    set "modifiedLine=!line:MicaApp=%executableName%!"
    echo !modifiedLine!
    endlocal
 ))  > "%outputFile%"



set "ResourcesPath=%micaPath%\MicaSetup\Resources\Images" 
copy "%CD%\build\icon.ico" "%ResourcesPath%\Favicon.ico" 
copy "%CD%\build\icon.png" "%ResourcesPath%\Favicon.png"
copy "%CD%\build\icon.png" "%ResourcesPath%\FaviconRaw.png"
copy "%CD%\build\logo-ins.ico" "%ResourcesPath%\FaviconSetup.ico"
copy "%CD%\build\logo-ins.png" "%ResourcesPath%\FaviconSetup.png"
copy "%CD%\build\logo-uins.ico" "%ResourcesPath%\FaviconUninst.ico"
copy "%CD%\build\logo-uins.png" "%ResourcesPath%\FaviconUninst.png"

@REM cmd /c "npm run build:win"

"%micaPath%/MicaSetup.Tools\7-Zip\7z" a "%micaPath%\MicaSetup\Resources\Setups\publish.7z" "%CD%\dist\win-unpacked\*" -t7z -mx=5 -mf=BCJ2 -r -y

set "RootPath=%CD%"
cd /d "%micaPath%" 

echo [trim font using fonttools]
cd /d .\MicaSetup.Tools\MicaFontTrim
cd subset
del subset.txt
dotnet run
cd ..
del HarmonyOS_Sans_SC_Regular.ttf
fonttools subset "fonts/HarmonyOS_Sans_SC_Regular.ttf" --text-file="subset/subset.txt" --output-file="HarmonyOS_Sans_SC_Regular.ttf"
copy /y HarmonyOS_Sans_SC_Regular.ttf ..\..\MicaSetup\Resources\Fonts\HarmonyOS_Sans_SC_Regular.ttf
cd /d "%micaPath%" 

@echo [build uninst using vs2022]
msbuild MicaSetup\MicaSetup.Uninst.csproj /t:Rebuild /p:Configuration=Release /p:DeployOnBuild=true /p:PublishProfile=FolderProfile /restore

@echo [build setup using vs2022]
copy /y .\MicaSetup\bin\Release\net472\MicaSetup.exe .\MicaSetup\Resources\Setups\Uninst.exe
msbuild MicaSetup\MicaSetup.csproj /t:Build /p:Configuration=Release /p:DeployOnBuild=true /p:PublishProfile=FolderProfile /restore

@echo [finish]
copy /y .\MicaSetup\bin\Release\net472\MicaSetup.exe "%RootPath%\dist\%productName% Setup.exe"


